home *** CD-ROM | disk | FTP | other *** search
/ The Atari Compendium / The Atari Compendium (Toad Computers) (1994).iso / files / prgtools / mint / utilit~1 / stlogin4.lzh / MORE.C < prev    next >
Encoding:
C/C++ Source or Header  |  1993-10-09  |  2.5 KB  |  115 lines

  1. /* UNIX like pager for Mint/TOS on ATARI ST.  Kees Lemmens; Juli 1992
  2.  
  3.    Also works on virtual screens (windows) and with pipes as in 
  4.    "ls -la | more"
  5.    If 'q' is pressed the pager aborts, any other key continues.
  6.    A / (slash) can be used to enter a string to search for.
  7.    
  8.    Uses environment variables LINES and COLUMNS (SYSV), or a default
  9.    of 25x80 if these are not found.
  10.  
  11.    Any questions or suggestions about this program can be send to:
  12.    lemmens@dv.twi.tudelft.nl
  13. */
  14.  
  15. #include <stdio.h>
  16. #include <stdlib.h>
  17. #include <string.h>
  18. #include <ctype.h>
  19. #include "ux_misc.h"
  20.  
  21. #define DEFCOLS   80
  22. #define DEFLINS   25
  23. #define MAXLEN    1000
  24.  
  25. void clearline(void)
  26. {    fprintf(stdout,"\r               \r");
  27. }
  28.  
  29. void getstring(char *str)
  30. {    while(read(1,str,1), *str != '\r') str++;
  31.     *str='\0';
  32. }
  33.  
  34. char putmore(void)
  35. {    char ch;
  36.  
  37.     fprintf(stdout,"  More ...");
  38.     read(1,&ch,1);
  39.     clearline();
  40.     return ch;
  41. }
  42.  
  43. void pagefile(FILE *in, int nrln, int nrcl)
  44. {    int cn=1;
  45.     size_t stl;
  46.     char *tmp,buffer[MAXLEN],str[100];
  47.  
  48.     while(!feof(in))
  49.     {    for(;cn<nrln && !feof(in);cn++)
  50.         {    *buffer='\0';
  51.             fgets(buffer,MAXLEN-1,in);
  52.             if((stl=strlen(buffer)) > nrcl)
  53.                 strcat(buffer+nrcl-1,"\n\0");
  54.             if(strrchr(buffer,'\n') == NULL)
  55.                 strcat(buffer+stl,"\n\0");
  56.             fprintf(stdout,buffer);
  57.         }
  58.         cn=1;
  59.         if (!feof(in))
  60.         {    switch(toupper(putmore()))
  61.             {    case 'Q':    putchar('\n');
  62.                             exit(0);
  63.                             break;
  64.                 case '/':    putchar('/');
  65.                             getstring(str); /* don't read pipe */
  66.                             do
  67.                             {    fgets(buffer,MAXLEN,in);
  68.                                 tmp=strstr(buffer,str);
  69.                             }while(tmp==NULL && !feof(in));
  70.                             if(tmp != NULL)
  71.                             {    clearline();
  72.                                 fprintf(stdout,buffer);
  73.                                 cn++;
  74.                             }
  75.                             else    return;
  76.                             break;
  77.             }
  78.         }
  79.     }
  80. }
  81.  
  82. void main(int argc,char *argv[]) 
  83. {   FILE *in;
  84.     char *tmp;
  85.     int nrln,nrcl,multiflag = 0;
  86.     char *streep= "\n----------\n";
  87.     
  88.     tmp  = getenv("LINES");
  89.     nrln = (tmp == NULL) ? DEFLINS : atoi(tmp);
  90.     tmp  = getenv("COLUMNS");
  91.     nrcl = (tmp == NULL) ? DEFCOLS : atoi(tmp);
  92.  
  93.     if (argc == 1)
  94.     {    pagefile(stdin,nrln,nrcl);
  95.         exit(0);
  96.     }
  97.     if(argc > 2)
  98.         multiflag = 1;    /* more than one file */
  99.  
  100.     while(argc-- > 1)
  101.     {    if(multiflag)
  102.         {    fprintf(stdout,"%s%s%s",streep,argv[argc],streep);
  103.             putmore();
  104.         }
  105.         ux2dos(argv[argc]);    /* convert slashes */
  106.         if ((in = fopen(argv[argc],"r")) == NULL)
  107.         {    fprintf(stderr,"\nCan't open <%s>\n",argv[argc]);
  108.             continue; 
  109.         }
  110.         pagefile(in,nrln,nrcl);
  111.         fclose(in);
  112.     }
  113.     exit(0);
  114. }
  115.